/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/


#include "../core.h"


/*-----------------------------------------------------------------------------
	Plane :
-----------------------------------------------------------------------------*/

//
//	EPlane::EPlane
//
EPlane::EPlane( void )
{
	a = 0;
	b = 0;
	c = 1;
	d = 0;
}


//
//	EPlane::EPlane
//
EPlane::EPlane( float a, float b, float c, float d )
{
	this->a = a;
	this->b = b;
	this->c = c;
	this->d = d;
}


//	
//	EPlane::EPlane
//
EPlane::EPlane( const float *p )
{
	a = p[0];
	b = p[1];
	c = p[2];
	d = p[3];
}


//
//	EPlane::EPlane
//
EPlane::EPlane( const EPlane &other )
{
	a = other.a;
	b = other.b;
	c = other.c;
	d = other.d;
}


//
//	EPlane::EPlane
//
EPlane	& EPlane::operator=( const EPlane &other )
{
	a = other.a;
	b = other.b;
	c = other.c;
	d = other.d;
	return *this;
}


//
//		EPlane::EPlane
//
bool EPlane::operator==( const EPlane &other )
{
	return (a == other.a &&
			b == other.b &&
			c == other.c &&
			d == other.d );
}


//
//
//
bool EPlane::operator!=( const EPlane &other )
{
	return !((*this)==other);
}


//
//	EPlane::classifyPoint
//
int EPlane::classifyPoint( const EPoint &point, float eps/* =0 */ ) const
{
	float	d	=	distance( point );
	if ( d < -eps )	return -1;
	if ( d >  eps )	return 1;
	return 0;	
}


//
//	EPlane::distance
//
float EPlane::distance( const EPoint &point ) const
{
	float length = sqrt(a*a + b*b + c*c);
	
	float aa = a / length;
	float bb = b / length;
	float cc = c / length;
	float dd = d / length;
	
	EVector pv(aa, bb, cc);
	
	return EMath::Dot(pv, EVector(point)) + dd;
}


//
//	EPlane::traceRay
//
bool EPlane::traceRay( const EPoint &origin, const EVector &dir, float &frac ) const
{
	float	o_dot_n	=	(origin.x * a) + (origin.y * b) + (origin.z * c);
	float	n_dot_r	=	(dir.x	  *	a) + (dir.y    * b)	+ (dir.z    * c);
	
	if (abs(n_dot_r) < FLOAT_EPSILON) {
		return false;
	}
	
	frac = - (d + o_dot_n) / n_dot_r;
	
	return true;
}


//
//	EPlane::traceSegment
//
bool EPlane::traceSegment( const EPoint &point0, const EPoint &point1, float &frac ) const
{
	bool r = traceRay( point0, point1 - point0, frac );
	
	if ( !r ) { 
		return false;
	}
	
	if ( frac >1 || frac < 0 ) {
		return false;
	}
	
	return true;
}


//
//	EPlane::normalize
//
EPlane EPlane::normalize( void ) const
{
	EPlane	p	=	*this;
	float len = sqrt(a*a + b*b + c*c);
	p.a /= len;
	p.b /= len;
	p.c /= len;
	p.d /= len;
	return p;
}

/*-----------------------------------------------------------------------------
	Plane stuff implementation :
-----------------------------------------------------------------------------*/

//
//	PlaneFromPointNormal
//
EPlane PlaneFromPointNormal( const EPoint &point, const EVector &normal )
{
	ASSERT(point.w!=0);
	ASSERT(normal.w==0);
	
	EVector norm_normal = normal.Normalize();

	float a = norm_normal.x;
	float b = norm_normal.y;
	float c = norm_normal.z;
	
	float d = - (a * point.x  +  b * point.y  +  c * point.z) / point.w;
	return EPlane(a,b,c,d);
}


//
//	PlaneFromPoints
//
EPlane PlaneFromPoints( const EPoint &p0, const EPoint &p1, const EPoint &p2 )
{
	EVector	v0	=	p1 - p0;
	EVector	v1	=	p2 - p0;
	EVector	n	=	EMath::Cross( v0, v1 );
	
	return PlaneFromPointNormal(p0, n);
}


//
//	PlaneIntersection
//
bool PlaneIntersection( EPoint &out_point, const EPlane &p0, const EPlane &p1, const EPlane &p2 )
{
#if 0
	EMatrix3	d	(
			p0.a,  p0.b,  p0.c,
			p1.a,  p1.b,  p1.c,
			p2.a,  p2.b,  p2.c
		);

	EMatrix3	dx	(
			-p0.d,  p0.b,  p0.c,
			-p1.d,  p1.b,  p1.c,
			-p2.d,  p2.b,  p2.c
		);

	EMatrix3	dy	(
			p0.a,  -p0.d,  p0.c,
			p1.a,  -p1.d,  p1.c,
			p2.a,  -p2.d,  p2.c
		);

	EMatrix3	dz	(
			p0.a,  p0.b,  -p0.d,
			p1.a,  p1.b,  -p1.d,
			p2.a,  p2.b,  -p2.d
		);
		
	float det   = Matrix3Det( d );

	if (det==0) {
		return false;	//	no solution
	}

	float det_x = Matrix3Det( dx );
	float det_y = Matrix3Det( dy );
	float det_z = Matrix3Det( dz );
		
	
	out_point.x = det_x / det;
	out_point.y = det_y / det;
	out_point.z = det_z / det;
	out_point.w = 1;
#endif
	return true;
}


//
//	PlaneClassify
//
int PlaneClassify( const EPlane &plane, const EPoint &point )
{
	EVector pv(plane.a, plane.b, plane.c);
	float dot = EMath::Dot(pv, EVector(point));
	if (dot<0)	return -1;
	if (dot>0)	return +1;
	return 0;
}


//
//	PlaneDistance
//
float PlaneDistance( const EPlane &plane, const EPoint &point )
{
	float a = plane.a;
	float b = plane.b;
	float c = plane.c;
	float d = plane.d;
	
	float length = sqrt(a*a + b*b + c*c);
	
	a /= length;
	b /= length;
	c /= length;
	d /= length;
	
	EVector pv(a, b, c);
	
	return EMath::Dot( pv, EVector(point) );
}


//
//	PlaneIsPointEnclosed
//
bool PlaneIsPointEnclosed( const EPoint &point, uint num_planes, const EPlane *planes, float point_normal_offset )
{
	for (uint i=0; i<num_planes; i++) {
		float dist = PlaneDistance(planes[i], point);
		dist += point_normal_offset;
		
		if (dist>0) {
			return false;
		}
	}
	
	return true;
}


//
//	PlaneTraceRayAgainstPlane
//
float PlaneTraceRayAgainstPlane( EPoint &out_point, const EPoint &origin, const EVector &dir, const EPlane &plane )
{
	float t;
	plane.traceRay( origin, dir, t );
	
	out_point = origin + dir * t;
	
	return t;
}




